home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / LWR74Y (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  9.0 KB  |  316 lines

  1. package com.sun.java.swing;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.Frame;
  7. import java.awt.Point;
  8. import java.awt.Rectangle;
  9. import java.awt.Toolkit;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.MouseMotionListener;
  13. import java.util.EventObject;
  14.  
  15. public class ToolTipManager extends MouseAdapter implements MouseMotionListener {
  16.    Timer enterTimer = new Timer(750, new insideTimerAction(this));
  17.    Timer exitTimer;
  18.    Timer insideTimer;
  19.    String toolTipText;
  20.    Point preferredLocation;
  21.    JComponent insideComponent;
  22.    MouseEvent mouseEvent;
  23.    boolean showImmediately;
  24.    static final ToolTipManager sharedInstance = new ToolTipManager();
  25.    Popup tipWindow;
  26.    JToolTip tip;
  27.    Rectangle popupRect = null;
  28.    boolean enabled = true;
  29.    boolean mouseAboveToolTip = false;
  30.    private boolean tipShowing = false;
  31.    private long timerEnter = 0L;
  32.    protected boolean lightWeightPopupEnabled = true;
  33.  
  34.    ToolTipManager() {
  35.       this.enterTimer.setRepeats(false);
  36.       this.exitTimer = new Timer(500, new outsideTimerAction(this));
  37.       this.exitTimer.setRepeats(false);
  38.       this.insideTimer = new Timer(4000, new stillInsideTimerAction(this));
  39.       this.insideTimer.setRepeats(false);
  40.    }
  41.  
  42.    static Frame frameForComponent(Component component) {
  43.       while(!(component instanceof Frame)) {
  44.          component = component.getParent();
  45.       }
  46.  
  47.       return (Frame)component;
  48.    }
  49.  
  50.    public int getDismissDelay() {
  51.       return this.insideTimer.getInitialDelay();
  52.    }
  53.  
  54.    public int getInitialDelay() {
  55.       return this.enterTimer.getInitialDelay();
  56.    }
  57.  
  58.    public int getReshowDelay() {
  59.       return this.exitTimer.getInitialDelay();
  60.    }
  61.  
  62.    void hideTipWindow() {
  63.       if (this.tipWindow != null) {
  64.          this.tipWindow.removeMouseListener(this);
  65.          this.tipWindow.hide();
  66.          this.tipWindow.dispose();
  67.          this.tipWindow = null;
  68.          this.tipShowing = false;
  69.          this.timerEnter = 0L;
  70.          this.tip.getUI().uninstallUI(this.tip);
  71.          this.tip = null;
  72.          this.insideTimer.stop();
  73.       }
  74.  
  75.    }
  76.  
  77.    public boolean isEnabled() {
  78.       return this.enabled;
  79.    }
  80.  
  81.    public boolean isLightWeightPopupEnabled() {
  82.       return this.lightWeightPopupEnabled;
  83.    }
  84.  
  85.    public void mouseDragged(MouseEvent event) {
  86.    }
  87.  
  88.    public void mouseEntered(MouseEvent event) {
  89.       if (!this.tipShowing || this.lightWeightPopupEnabled || System.currentTimeMillis() - this.timerEnter >= 100L) {
  90.          if (((EventObject)event).getSource() != this.tipWindow) {
  91.             JComponent component = (JComponent)((EventObject)event).getSource();
  92.             this.toolTipText = component.getToolTipText(event);
  93.             this.preferredLocation = component.getToolTipLocation(event);
  94.             this.exitTimer.stop();
  95.             Point location = event.getPoint();
  96.             if (location.x >= 0 && location.x < component.getWidth() && location.y >= 0 && location.y < component.getHeight()) {
  97.                if (this.insideComponent != null) {
  98.                   this.enterTimer.stop();
  99.                   this.insideComponent = null;
  100.                }
  101.  
  102.                ((Component)component).addMouseMotionListener(this);
  103.                this.insideComponent = component;
  104.                if (this.toolTipText != null) {
  105.                   this.mouseEvent = event;
  106.                   if (this.showImmediately) {
  107.                      this.showTipWindow();
  108.                   } else {
  109.                      this.enterTimer.start();
  110.                   }
  111.                }
  112.  
  113.             }
  114.          }
  115.       }
  116.    }
  117.  
  118.    public void mouseExited(MouseEvent event) {
  119.       if (!this.tipShowing || this.lightWeightPopupEnabled || System.currentTimeMillis() - this.timerEnter >= 100L) {
  120.          boolean shouldHide = true;
  121.          if (((EventObject)event).getSource() == this.tipWindow) {
  122.             Container insideComponentWindow = this.insideComponent.getTopLevelAncestor();
  123.             Rectangle b = this.tipWindow.getBounds();
  124.             Point location = event.getPoint();
  125.             location.x += b.x;
  126.             location.y += b.y;
  127.             b = ((Component)insideComponentWindow).getBounds();
  128.             location.x -= b.x;
  129.             location.y -= b.y;
  130.             location = SwingUtilities.convertPoint((Component)null, location, this.insideComponent);
  131.             if (location.x >= 0 && location.x < this.insideComponent.getWidth() && location.y >= 0 && location.y < this.insideComponent.getHeight()) {
  132.                shouldHide = false;
  133.             } else {
  134.                shouldHide = true;
  135.             }
  136.          } else if (((EventObject)event).getSource() == this.insideComponent && this.tipWindow != null) {
  137.             Point location = SwingUtilities.convertPoint(this.insideComponent, event.getPoint(), (Component)null);
  138.             Rectangle bounds = this.insideComponent.getTopLevelAncestor().getBounds();
  139.             location.x += bounds.x;
  140.             location.y += bounds.y;
  141.             bounds = this.tipWindow.getBounds();
  142.             if (location.x >= bounds.x && location.x < bounds.x + bounds.width && location.y >= bounds.y && location.y < bounds.y + bounds.height) {
  143.                shouldHide = false;
  144.             } else {
  145.                shouldHide = true;
  146.             }
  147.          }
  148.  
  149.          if (shouldHide) {
  150.             this.enterTimer.stop();
  151.             if (this.insideComponent != null) {
  152.                this.insideComponent.removeMouseMotionListener(this);
  153.             }
  154.  
  155.             this.insideComponent = null;
  156.             this.toolTipText = null;
  157.             this.mouseEvent = null;
  158.             this.hideTipWindow();
  159.             this.exitTimer.start();
  160.          }
  161.  
  162.       }
  163.    }
  164.  
  165.    public void mouseMoved(MouseEvent event) {
  166.       JComponent component = (JComponent)((EventObject)event).getSource();
  167.       String newText = component.getToolTipText(event);
  168.       Point newPreferredLocation = component.getToolTipLocation(event);
  169.       if (newText == null && newPreferredLocation == null) {
  170.          this.toolTipText = null;
  171.          this.preferredLocation = null;
  172.          this.mouseEvent = null;
  173.          this.hideTipWindow();
  174.          this.enterTimer.stop();
  175.          this.exitTimer.start();
  176.       } else {
  177.          this.mouseEvent = event;
  178.          if ((newText != null && newText.equals(this.toolTipText) || newText == null) && (newPreferredLocation != null && newPreferredLocation.equals(this.preferredLocation) || newPreferredLocation == null)) {
  179.             if (this.tipWindow != null) {
  180.                this.insideTimer.restart();
  181.             } else {
  182.                this.enterTimer.restart();
  183.             }
  184.          } else {
  185.             this.toolTipText = newText;
  186.             this.preferredLocation = newPreferredLocation;
  187.             if (this.showImmediately) {
  188.                this.hideTipWindow();
  189.                this.showTipWindow();
  190.             } else {
  191.                this.enterTimer.restart();
  192.             }
  193.          }
  194.       }
  195.  
  196.    }
  197.  
  198.    public void mousePressed(MouseEvent event) {
  199.       this.hideTipWindow();
  200.       this.enterTimer.stop();
  201.       this.showImmediately = false;
  202.    }
  203.  
  204.    private boolean popupFit(Rectangle popupRectInScreen, Component invoker) {
  205.       if (invoker != null) {
  206.          for(Container parent = invoker.getParent(); parent != null; parent = ((Component)parent).getParent()) {
  207.             if (parent instanceof JFrame || parent instanceof JDialog) {
  208.                return SwingUtilities.isRectangleContainingRectangle(((Component)parent).getBounds(), popupRectInScreen);
  209.             }
  210.  
  211.             if (parent instanceof JApplet) {
  212.                Rectangle r = ((Component)parent).getBounds();
  213.                Point p = ((Component)parent).getLocationOnScreen();
  214.                r.x = p.x;
  215.                r.y = p.y;
  216.                return SwingUtilities.isRectangleContainingRectangle(r, popupRectInScreen);
  217.             }
  218.  
  219.             if (parent instanceof Frame) {
  220.                return SwingUtilities.isRectangleContainingRectangle(((Component)parent).getBounds(), popupRectInScreen);
  221.             }
  222.          }
  223.       }
  224.  
  225.       return false;
  226.    }
  227.  
  228.    public void registerComponent(JComponent component) {
  229.       ((Component)component).removeMouseListener(this);
  230.       ((Component)component).addMouseListener(this);
  231.    }
  232.  
  233.    public void setDismissDelay(int microSeconds) {
  234.       this.insideTimer.setInitialDelay(microSeconds);
  235.    }
  236.  
  237.    public void setEnabled(boolean flag) {
  238.       this.enabled = flag;
  239.       if (!flag) {
  240.          this.hideTipWindow();
  241.       }
  242.  
  243.    }
  244.  
  245.    public void setInitialDelay(int microSeconds) {
  246.       this.enterTimer.setInitialDelay(microSeconds);
  247.    }
  248.  
  249.    public void setLightWeightPopupEnabled(boolean aFlag) {
  250.       this.lightWeightPopupEnabled = aFlag;
  251.    }
  252.  
  253.    public void setReshowDelay(int microSeconds) {
  254.       this.exitTimer.setInitialDelay(microSeconds);
  255.    }
  256.  
  257.    public static ToolTipManager sharedInstance() {
  258.       return sharedInstance;
  259.    }
  260.  
  261.    void showTipWindow() {
  262.       if (this.insideComponent != null && this.insideComponent.isShowing()) {
  263.          if (this.enabled) {
  264.             Point screenLocation = this.insideComponent.getLocationOnScreen();
  265.             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  266.             Point location = new Point();
  267.             this.hideTipWindow();
  268.             this.tip = this.insideComponent.createToolTip();
  269.             this.tip.setTipText(this.toolTipText);
  270.             Dimension size = this.tip.getPreferredSize();
  271.             if (this.lightWeightPopupEnabled) {
  272.                this.tipWindow = new JPanelPopup(this, this.tip, size);
  273.             } else {
  274.                this.tipWindow = new PanelPopup(this, this.tip, size);
  275.             }
  276.  
  277.             this.tipWindow.addMouseListener(this);
  278.             if (this.preferredLocation != null) {
  279.                location.x = screenLocation.x + this.preferredLocation.x;
  280.                location.y = screenLocation.y + this.preferredLocation.y;
  281.             } else {
  282.                location.x = screenLocation.x + this.mouseEvent.getX();
  283.                location.y = screenLocation.y + this.mouseEvent.getY() + 20;
  284.                if (location.x + size.width > screenSize.width) {
  285.                   location.x -= size.width;
  286.                }
  287.  
  288.                if (location.y + size.height > screenSize.height) {
  289.                   location.y -= size.height + 20;
  290.                }
  291.             }
  292.  
  293.             if (this.popupRect == null) {
  294.                this.popupRect = new Rectangle(location.x, location.y, this.tipWindow.getBounds().width, this.tipWindow.getBounds().height);
  295.             } else {
  296.                this.popupRect.setBounds(location.x, location.y, this.tipWindow.getBounds().width, this.tipWindow.getBounds().height);
  297.             }
  298.  
  299.             if (!this.popupFit(this.popupRect, this.insideComponent)) {
  300.                this.tipWindow = new WindowPopup(this, frameForComponent(this.insideComponent), this.tip, size);
  301.             }
  302.  
  303.             this.tipWindow.show(this.insideComponent, location.x, location.y);
  304.             this.insideTimer.start();
  305.             this.timerEnter = System.currentTimeMillis();
  306.             this.tipShowing = true;
  307.          }
  308.  
  309.       }
  310.    }
  311.  
  312.    public void unregisterComponent(JComponent component) {
  313.       ((Component)component).removeMouseListener(this);
  314.    }
  315. }
  316.